package com.hjxr.quota.datasource.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.hjxr.common.utils.StringUtils;
import com.hjxr.framework.aspectj.lang.annotation.Log;
import com.hjxr.framework.aspectj.lang.enums.BusinessType;
import com.hjxr.quota.datasource.domain.QuotaDimension;
import com.hjxr.quota.datasource.service.IQuotaDimensionService;
import com.hjxr.quota.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 维度管理前端控制器
 *
 * @author sun
 * @date 2020-09-16
 */
@RestController
@RequestMapping("/quota/dimension")
@Api(tags = "维度管理")
public class QuotaDimensionController extends ApiController {
    @Autowired
    private IQuotaDimensionService quotaDimensionService;

    /**
     * 分页查询所有数据
     */
    @PreAuthorize("@ss.hasPermi('quota:dimension:list')")
    @GetMapping("/list")
    @ApiOperation("分页查询所有数据")
    @ApiImplicitParams(
            {@ApiImplicitParam(paramType = "query", dataType = "String", name = "current", value = "当前页", defaultValue = "1", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "size", value = "一页大小", defaultValue = "10", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "Boolean", name = "ifCount", value = "是否查询总数", defaultValue = "true"),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "ascs", value = "升序字段，多个用逗号分隔"),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "descs", value = "降序字段，多个用逗号分隔")
            })
    public R<IPage<QuotaDimension>> selectAll() {
        BaseForm form = new BaseForm();
        QueryWrapper<QuotaDimension> query = (QueryWrapper<QuotaDimension>) form.pageQueryWrapper(
                form.getParameters(), new QueryWrapper<QuotaDimension>());
        return success(quotaDimensionService.page(form.getPlusPagingQueryEntity(), query));
    }

    @GetMapping("/listBySearch")
    @ApiOperation("分页加关键词搜索所有数据")
    public R<PageUtil> selectAllBySearch(@RequestParam Map<String, Object> params) {
        return success(quotaDimensionService.selectAllBySearch(params));
    }

    /**
     * 获取维度详细信息
     */
    @ApiOperation("获取维度详细信息")
    @PreAuthorize("@ss.hasPermi('quota:dimension:query')")
    @GetMapping(value = "{id}")
    public R<QuotaDimension> getInfo(@PathVariable Serializable id) {
        return success(quotaDimensionService.getById(id));
    }

    /**
     * 新增维度
     */
    @ApiOperation("新增维度")
    @PreAuthorize("@ss.hasPermi('quota:dimension:add')")
//  @Log(title = "新增维度", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Boolean> add(@RequestBody QuotaDimension quotaDimension) {
        return success(quotaDimensionService.save(quotaDimension));
    }

    /**
     * 修改维度
     */
    @ApiOperation("修改维度")
    @PreAuthorize("@ss.hasPermi('quota:dimension:edit')")
    @Log(title = "修改维度", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Boolean> update(@RequestBody QuotaDimension quotaDimension) {
        UpdateWrapper<QuotaDimension> wrapper = new UpdateWrapper<>();
        wrapper.eq("ID", quotaDimension.getId());
        if (StringUtils.isNotBlank(quotaDimension.getStatus())) {
            wrapper.set("STATUS", quotaDimension.getStatus());
        }
        return success(quotaDimensionService.update(quotaDimension, wrapper));
    }

    /**
     * 删除维度
     */
    @ApiOperation("删除维度")
    @PreAuthorize("@ss.hasPermi('quota:dimension:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping
    public R<Boolean> remove(@RequestParam("idList") List<Integer> ids) {
        return success(quotaDimensionService.removeByIds(ids));
    }
}
